package com.mapapp.flutter_mapapp.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.mapapp.flutter_mapapp.R;

/**
 *
 * 自定义 进度条
 */
public class CustomizedProgressBar extends View {
    private float maxCount = 100; //进度条最大值
    private float currentCount = 0; //进度条当前值
    private Paint mPaintBg;
    private Paint mPaintContent;
    private Paint mPaintLast;
    private int mWidth,mHeight;
    private Context mContext;
    private int color1 = 0xFFFF9326;

    public CustomizedProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    public CustomizedProgressBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public CustomizedProgressBar(Context context) {
        super(context);
        initView(context);
    }

    private void initView(Context context) {
        mContext=context;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mPaintBg = new Paint();
        mPaintBg.setColor(Color.BLACK);
        mPaintBg.setStrokeWidth(10);
        mPaintBg.setStyle(Paint.Style.STROKE);
        RectF rectBg = new RectF(0, 0, mWidth, mHeight);
        canvas.drawRect(rectBg, mPaintBg);//绘制 最外面的大 圆角矩形，背景为白色

        mPaintContent = new Paint();
        mPaintContent.setAntiAlias(true);
        mPaintContent.setStyle(Paint.Style.FILL_AND_STROKE);
        float section = currentCount/maxCount; //进度条的比例
        RectF rectProgressBg = new RectF(0, 0, mWidth*section, mHeight);
        //Paint设置setColor(白色无透明)和setShader，只让setShader生效；不然前面setColor设置了透明度，透明度会生效，和setShader效果叠加
        mPaintContent.setColor(getResources().getColor(R.color.white));
        mPaintContent.setShader(getLinearGradient());
        canvas.drawRect(rectProgressBg, mPaintContent); //最左边的圆角矩形

        if (maxCount != currentCount && 0 != currentCount){ //如果不是100%，绘制第三段矩形
            mPaintLast = new Paint();
            mPaintLast.setAntiAlias(true);
            mPaintLast.setStyle(Paint.Style.FILL_AND_STROKE);
            mPaintLast.setColor(getResources().getColor(R.color.colorAccent)); //设置边框背景颜色
            RectF rectProgressBg2 = new RectF(mWidth*section, 0, mWidth, mHeight);
            canvas.drawRect(rectProgressBg2, mPaintLast);
        }
    }

    private LinearGradient linearGradient;
    private LinearGradient getLinearGradient(){
        if(linearGradient==null){
            linearGradient = new LinearGradient(0, 0, getWidth(), mHeight,
                    new int[]{color1,color1}, null, Shader.TileMode.CLAMP); //根据R文件中的id获取到color
        }
        return linearGradient;
    }

    private int dipToPx(int dip) {
        float scale = getContext().getResources().getDisplayMetrics().density;
        return (int) (dip * scale + 0.5f * (dip >= 0 ? 1 : -1));
    }

    /***
     * 设置当前的进度值
     * @param currentCount 当前进度值
     */
    public void setCurrentCount(float currentCount) {
        this.currentCount = currentCount > maxCount ? maxCount : currentCount;
        invalidate();
    }

    /***
     * 设置渐变颜色
     */
    public void setGradualColor(int color1){
        this.color1 = color1;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
        if (widthSpecMode == MeasureSpec.EXACTLY || widthSpecMode == MeasureSpec.AT_MOST) {
            mWidth = widthSpecSize;
        } else {
            mWidth = 0;
        }
        if (heightSpecMode == MeasureSpec.AT_MOST || heightSpecMode == MeasureSpec.UNSPECIFIED) {
            mHeight = dipToPx(18);
        } else {
            mHeight = heightSpecSize;
        }
        setMeasuredDimension(mWidth, mHeight);
    }
}